<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- tree布局 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDelete(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹框 -->
        <el-dialog
          v-if="showDialog"
          :title="isEdit?'编辑':'新增'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="hClose"
        >
          <addor-edit
            ref="addoredit"
            :cur-id="curId"
            :is-edit="isEdit"
            :oringin-list="oringinList"
            @success="success"
            @Cancel="Cancel"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getdepartment, deldepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  name: 'Department',
  components: {
    AddorEdit
  },
  data() {
    return {
      // 是否显弹框
      showDialog: false,
      // 部门列表
      list: [],
      // 是否是编辑框
      isEdit: false,
      // 当前id
      curId: '',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 部分数据数组
      oringinList: []
    }
  },
  created() {
    // 刷新页面
    this.setdepartment()
  },
  methods: {
    // 查询企业的部门列表
    async setdepartment() {
      try {
        const res = await getdepartment()
        // console.log(res.data.data.depts, '拿数据')
        // 删除数组第一个
        res.data.data.depts.shift()
        this.oringinList = res.data.data.depts.map(({ id, pid, code, name }) => {
          return { id, pid, code, name }
        })
        this.list = tranListToTreeData(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加子部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 添加或修改成功
    success() {
      this.showDialog = false
      this.setdepartment()
    },
    // 删除部门
    hDelete(id) {
      this.$confirm('删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await deldepartment(id)
          // console.log(res, '删除部门')
          this.setdepartment()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子组件取消按钮
    Cancel() {
      this.showDialog = false
    },
    // 关闭弹窗后清除表单内容
    hClose() {
      this.$refs.addoredit.resetFields()
    }
  }
}
</script>

<style>

</style>
